#pragma once
namespace my_vector {
	template<class T>
	class vector {
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
			:start(nullptr)
			,finnal(nullptr)
			,endofstorage(nullptr)
		{}
		size_t size() const
		{
			return finnal - start;
		}
		size_t capacity() const
		{
			return endofstorage - start;
		}
		//迭代器的实现
		iterator begin()
		{
			return start;
		}
		const_iterator begin() const
		{
			return start;
		}

		iterator end()
		{
			return finnal;
		}
		const_iterator end() const
		{
			return finnal;
		}

		//非静态
		T& operator[](size_t pos)
		{
			assert(pos < size());
		    //return *(start + pos);
			return start[pos];
		}
		//静态
		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			//return *(start + pos);
			return start[pos];
		}
		
		//开辟空间
		void reserve(size_t n)
		{
			if (n > capacity())
			{	//开辟空间
				T* tmp = new T[n];
				if (start)
				{
					memcpy(tmp, start, sizeof(T)*size());

				}
				size_t len = size();
				delete[] start;
				start = tmp;
				finnal = start + len;
				endofstorage = start + n;
			}
		}
		//resize的实现
		void resize(size_t n, T val =  T())  //这儿是个匿名对象调用初始化
			//resize有三种情况 n<=size 缩小size   size<n<=capacity 将后面的赋值
			//    n>capacity 扩容
		{
			////先扩容
			//if (n > size())
			//{
			//	reserve(n);
			//	while (finnal < start+n)
			//	{
			//		*finnal = val;
			//		finnal++;
			//	}
			//}
			//else 
			//{
			//	finnal = start + n;
			//}

			if (n > capacity())
			{
				reserve(n);
			}
			if (n > size())
			{
				while (finnal < start+n)
				{
					*finnal = val;
					finnal++;
				}
			}
			else
			{
				finnal = start + n;
			}

		}
		//insert()的实现  主语i这里会发生迭代器失效，因为pos传过来的位置是不变的，
		//而v在扩容之后，地址发生变化，所以在原来的位置上找不pos了，
		//造成访问野指针，因此报错
		//野指针的检查是抽查，编译器不一定检测得出来
		void insert(iterator pos, const T& val)
		{
			assert(pos <= finnal);
			
			if (finnal == endofstorage)
			{
				size_t old = pos - start;
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
				pos = start + old;
			}
			iterator end = finnal;
			while (end > pos)
			{
				*(end) = *(end - 1);
				end--;
			}
			finnal++;
			*pos = val;
		}

		//erase的实现
		iterator erase(iterator pos)
		{
			assert(pos < finnal);
			//从后往前挪动数据
			while (pos < finnal)
			{
				*pos = *(pos + 1);
				pos++;
			}
			finnal--;
			return start;
		}

		iterator erase(iterator pos, const size_t n)
		{
			assert(pos < finnal);
			if (n > finnal - pos)
			{
				finnal = pos;
			}
			else
			{
				while (pos + n < finnal)
				{
					*(pos) = *(pos + n);
					pos++;
				}
				finnal = pos;
			}
			
			return start;
		}

		//swap()的实现
		void swap(vector<T>& v)
		{

		}

		void push_back(const T& n)  //这儿可以使用const T& n
		{
			if (finnal == endofstorage)
			{
				//扩容
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
			}
			*finnal = n;
			++finnal;
		}

		bool empty() const
		{
			return start == finnal;
		}

		T& front()
		{
			return *start;
		}
		T& front() const
		{
			return *start;
		}

		T& back()
		{
			return *(finnal-1);
		}
		T& back() const
		{
			return *(finnal - 1);
		}

		void pop_back()
		{
			assert(empty());
			finnal--;
		}

	private:
		iterator start;
		iterator finnal;
		iterator endofstorage;

	};

	void vector_test()
	{
		vector<int> v;
		//v.resize(10);
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		v.push_back(6);
		v.push_back(7);
		v.push_back(8);
		v.push_back(9);

		for (auto ve : v)
		{
			cout << ve << " ";
		}
		cout << endl;
		for (size_t i = 0; i < v.size(); ++i)
		{
			cout << v[i] << " ";
		}
		cout << endl;
		cout << v.size()<<" "<< v.capacity() << endl;
		v.resize(10,1);
		for (size_t i = 0; i < v.size(); ++i)
		{
			cout << v[i] << " ";
		}
		cout << endl;
		cout << v.size() << " " << v.capacity() << endl;
		cout << v.front() << "  " << v.back() << endl;

		v.erase(v.begin(),11);
		for (size_t i = 0; i < v.size(); ++i)
		{
			cout << v[i] << " ";
		}
		cout << endl;
	}
	//迭代器失效，在reserve之后，内存位置变换了，
	void vector_test1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		v.push_back(6);
		v.insert(v.begin()+6, 0);
		for (size_t i = 0; i < v.size(); ++i)
		{
			cout << v[i] << " ";
		}
		cout << endl;
		vector<int> v1;
		//v.resize(10);
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.swap(v);
		for (size_t i = 0; i < v.size(); ++i)
		{
			cout << v[i] << " ";
		}


	}
	//迭代器失效，在reserve之后，内存位置变换了，
	void vector_test2()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		for (size_t i = 0; i < v.size(); ++i)
		{
			cout << v[i] << " ";
		}
		cout << endl;

		v.insert(v.begin(), 0);
		for (auto ve : v)
		{
			cout << ve << " ";
		}
		cout << endl;
		//find可以iostream调用库里面的
	}


}